home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / mac / DirectX SDK / DXSDK / samples / Multimedia / Common / include / d3dsaver.h < prev    next >
C/C++ Source or Header  |  2001-10-08  |  13KB  |  315 lines

  1. //-----------------------------------------------------------------------------
  2. // File: D3DSaver.h
  3. //
  4. // Desc: Framework for screensavers that use Direct3D 8.0.
  5. //
  6. // Copyright (c) 2000 Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #ifndef _D3DSAVER_H
  9. #define _D3DSAVER_H
  10.  
  11.  
  12. //-----------------------------------------------------------------------------
  13. // Error codes
  14. //-----------------------------------------------------------------------------
  15. enum APPMSGTYPE { MSG_NONE, MSGERR_APPMUSTEXIT, MSGWARN_SWITCHEDTOREF };
  16.  
  17. #define D3DAPPERR_NODIRECT3D          0x82000001
  18. #define D3DAPPERR_NOWINDOW            0x82000002
  19. #define D3DAPPERR_NOCOMPATIBLEDEVICES 0x82000003
  20. #define D3DAPPERR_NOWINDOWABLEDEVICES 0x82000004
  21. #define D3DAPPERR_NOHARDWAREDEVICE    0x82000005
  22. #define D3DAPPERR_HALNOTCOMPATIBLE    0x82000006
  23. #define D3DAPPERR_NOWINDOWEDHAL       0x82000007
  24. #define D3DAPPERR_NODESKTOPHAL        0x82000008
  25. #define D3DAPPERR_NOHALTHISMODE       0x82000009
  26. #define D3DAPPERR_NONZEROREFCOUNT     0x8200000a
  27. #define D3DAPPERR_MEDIANOTFOUND       0x8200000b
  28. #define D3DAPPERR_RESIZEFAILED        0x8200000c
  29. #define D3DAPPERR_INITDEVICEOBJECTSFAILED 0x8200000d
  30. #define D3DAPPERR_CREATEDEVICEFAILED  0x8200000e
  31. #define D3DAPPERR_NOPREVIEW           0x8200000f
  32.  
  33.  
  34. //-----------------------------------------------------------------------------
  35. // Constants
  36. //-----------------------------------------------------------------------------
  37. #define MAX_DISPLAYS 9
  38. #define NO_ADAPTER 0xffffffff
  39. #define NO_MONITOR 0xffffffff
  40.  
  41.  
  42. //***************************************************************************************
  43. // Modes of operation for screensaver
  44. enum SaverMode
  45. {
  46.     sm_config,         // Config dialog box
  47.     sm_preview,        // Mini preview window in Display Properties dialog
  48.     sm_full,           // Full-on screensaver mode
  49.     sm_test,           // Test mode
  50.     sm_passwordchange  // Change password
  51. };
  52.  
  53.  
  54. // Prototype for VerifyScreenSavePwd() in password.cpl, used on Win9x
  55. typedef BOOL (PASCAL * VERIFYPWDPROC) (HWND);
  56.  
  57.  
  58. //-----------------------------------------------------------------------------
  59. // Name: struct D3DModeInfo
  60. // Desc: Structure for holding information about a display mode
  61. //-----------------------------------------------------------------------------
  62. struct D3DModeInfo
  63. {
  64.     DWORD      Width;      // Screen width in this mode
  65.     DWORD      Height;     // Screen height in this mode
  66.     D3DFORMAT  Format;     // Pixel format in this mode
  67.     DWORD      dwBehavior; // Hardware / Software / Mixed vertex processing
  68.     D3DFORMAT  DepthStencilFormat; // Which depth/stencil format to use with this mode
  69. };
  70.  
  71.  
  72.  
  73.  
  74. //-----------------------------------------------------------------------------
  75. // Name: struct D3DWindowedModeInfo
  76. // Desc: Structure for holding information about a display mode
  77. //-----------------------------------------------------------------------------
  78. struct D3DWindowedModeInfo
  79. {
  80.     D3DFORMAT  DisplayFormat;
  81.     D3DFORMAT  BackBufferFormat;
  82.     DWORD      dwBehavior; // Hardware / Software / Mixed vertex processing
  83.     D3DFORMAT  DepthStencilFormat; // Which depth/stencil format to use with this mode
  84. };
  85.  
  86.  
  87.  
  88.  
  89. //-----------------------------------------------------------------------------
  90. // Name: struct D3DDeviceInfo
  91. // Desc: Structure for holding information about a Direct3D device, including
  92. //       a list of modes compatible with this device
  93. //-----------------------------------------------------------------------------
  94. struct D3DDeviceInfo
  95. {
  96.     // Device data
  97.     D3DDEVTYPE   DeviceType;      // Reference, HAL, etc.
  98.     D3DCAPS8     d3dCaps;         // Capabilities of this device
  99.     const TCHAR* strDesc;         // Name of this device
  100.     BOOL         bCanDoWindowed;  // Whether this device can work in windowed mode
  101.  
  102.     // Modes for this device
  103.     DWORD        dwNumModes;
  104.     D3DModeInfo  modes[150];
  105.  
  106.     // Current state
  107.     DWORD        dwCurrentMode;
  108.     BOOL         bWindowed;
  109.     D3DMULTISAMPLE_TYPE MultiSampleType;
  110. };
  111.  
  112.  
  113.  
  114.  
  115. //-----------------------------------------------------------------------------
  116. // Name: struct D3DAdapterInfo
  117. // Desc: Structure for holding information about an adapter, including a list
  118. //       of devices available on this adapter
  119. //-----------------------------------------------------------------------------
  120. struct D3DAdapterInfo
  121. {
  122.     // Adapter data
  123.     DWORD          iMonitor; // Which MonitorInfo corresponds to this adapter
  124.     D3DADAPTER_IDENTIFIER8 d3dAdapterIdentifier;
  125.     D3DDISPLAYMODE d3ddmDesktop;      // Desktop display mode for this adapter
  126.  
  127.     // Devices for this adapter
  128.     DWORD          dwNumDevices;
  129.     D3DDeviceInfo  devices[3];
  130.     BOOL           bHasHAL;
  131.     BOOL           bHasAppCompatHAL;
  132.     BOOL           bHasSW;
  133.     BOOL           bHasAppCompatSW;
  134.  
  135.     // User's preferred mode settings for this adapter
  136.     DWORD          dwUserPrefWidth;
  137.     DWORD          dwUserPrefHeight;
  138.     D3DFORMAT      d3dfmtUserPrefFormat;
  139.     BOOL           bLeaveBlack;  // If TRUE, don't render to this display
  140.     BOOL           bDisableHW;   // If TRUE, don't use HAL on this display
  141.  
  142.     // Current state
  143.     DWORD          dwCurrentDevice;
  144.     HWND           hWndDevice;
  145. };
  146.  
  147.  
  148.  
  149.  
  150. //-----------------------------------------------------------------------------
  151. // Name: struct MonitorInfo
  152. // Desc: Structure for holding information about a monitor
  153. //-----------------------------------------------------------------------------
  154. struct MonitorInfo
  155. {
  156.     TCHAR          strDeviceName[128];
  157.     TCHAR          strMonitorName[128];
  158.     HMONITOR       hMonitor;
  159.     RECT           rcScreen;
  160.     DWORD          iAdapter; // Which D3DAdapterInfo corresponds to this monitor
  161.     HWND           hWnd;
  162.  
  163.     // Error message state
  164.     FLOAT          xError;
  165.     FLOAT          yError;
  166.     FLOAT          widthError;
  167.     FLOAT          heightError;
  168.     FLOAT          xVelError;
  169.     FLOAT          yVelError;
  170. };
  171.  
  172.  
  173.  
  174.  
  175. //-----------------------------------------------------------------------------
  176. // Name: struct RenderUnit
  177. // Desc: 
  178. //-----------------------------------------------------------------------------
  179. struct RenderUnit
  180. {
  181.     UINT                  iAdapter;
  182.     UINT                  iMonitor;
  183.     D3DDEVTYPE            DeviceType;      // Reference, HAL, etc.
  184.     DWORD                 dwBehavior;
  185.     IDirect3DDevice8*     pd3dDevice;
  186.     D3DPRESENT_PARAMETERS d3dpp;
  187.     BOOL                  bDeviceObjectsInited; // InitDeviceObjects was called
  188.     BOOL                  bDeviceObjectsRestored; // RestoreDeviceObjects was called
  189.     TCHAR                 strDeviceStats[90];// String to hold D3D device stats
  190.     TCHAR                 strFrameStats[40]; // String to hold frame stats
  191. };
  192.  
  193.  
  194.  
  195.  
  196. //-----------------------------------------------------------------------------
  197. // Name: class CD3DScreensaver
  198. // Desc: D3D screensaver class
  199. //-----------------------------------------------------------------------------
  200. class CD3DScreensaver
  201. {
  202. public:
  203.                     CD3DScreensaver();
  204.  
  205.     virtual HRESULT Create( HINSTANCE hInstance );
  206.     virtual INT     Run();
  207.     HRESULT         DisplayErrorMsg( HRESULT hr, DWORD dwType = 0 );
  208.  
  209. protected:
  210.     SaverMode       ParseCommandLine( TCHAR* pstrCommandLine );
  211.     VOID            ChangePassword();
  212.     HRESULT         DoSaver();
  213.  
  214.     virtual VOID    DoConfig() { }
  215.     virtual VOID    ReadSettings() {};
  216.     VOID            ReadScreenSettings( HKEY hkeyParent );
  217.     VOID            WriteScreenSettings( HKEY hkeyParent );
  218.  
  219.     virtual VOID    DoPaint( HWND hwnd, HDC hdc );
  220.     HRESULT         Initialize3DEnvironment();
  221.     VOID            Cleanup3DEnvironment();
  222.     HRESULT         Render3DEnvironment();
  223.     static LRESULT CALLBACK SaverProcStub( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
  224.     virtual LRESULT SaverProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
  225.     VOID            InterruptSaver();
  226.     VOID            ShutdownSaver();
  227.     VOID            DoScreenSettingsDialog( HWND hwndParent );
  228.     static INT_PTR CALLBACK ScreenSettingsDlgProcStub( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
  229.     INT_PTR         ScreenSettingsDlgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
  230.     VOID            SetupAdapterPage(HWND hWnd);
  231.  
  232.     HRESULT         CreateSaverWindow();
  233.     HRESULT         BuildDeviceList();
  234.     BOOL            FindDepthStencilFormat( UINT iAdapter, D3DDEVTYPE DeviceType,
  235.                         D3DFORMAT TargetFormat, D3DFORMAT* pDepthStencilFormat );
  236.     HRESULT         CheckWindowedFormat( UINT iAdapter, D3DWindowedModeInfo* pD3DWindowedModeInfo );
  237.     HRESULT         CreateFullscreenRenderUnit( RenderUnit* pRenderUnit );
  238.     HRESULT         CreateWindowedRenderUnit( RenderUnit* pRenderUnit );
  239.     BOOL            FindNextLowerMode( D3DDeviceInfo* pD3DDeviceInfo );
  240.     VOID            SwitchToRenderUnit( UINT iRenderUnit );
  241.     VOID            BuildProjectionMatrix( FLOAT fNear, FLOAT fFar, D3DXMATRIX* pMatrix );
  242.     HRESULT         SetProjectionMatrix( FLOAT fNear, FLOAT fFar );
  243.     virtual VOID    UpdateDeviceStats();
  244.     virtual VOID    UpdateFrameStats();
  245.     virtual BOOL    GetTextForError( HRESULT hr, TCHAR* pszError, DWORD dwNumChars );
  246.     VOID            UpdateErrorBox();
  247.     VOID            EnumMonitors( VOID );
  248.     BOOL            GetBestAdapter( DWORD* piAdapter );
  249.  
  250.     virtual VOID    SetDevice( UINT iDevice )                  { }
  251.     virtual HRESULT RegisterSoftwareDevice()                   { return S_OK; }
  252.     virtual HRESULT ConfirmDevice(D3DCAPS8* pCaps, DWORD dwBehavior, 
  253.                                   D3DFORMAT fmtBackBuffer)     { return S_OK; }
  254.     virtual HRESULT ConfirmMode( LPDIRECT3DDEVICE8 pd3dDev )   { return S_OK; }
  255.     virtual HRESULT OneTimeSceneInit()                         { return S_OK; }
  256.     virtual HRESULT InitDeviceObjects()                        { return S_OK; }
  257.     virtual HRESULT RestoreDeviceObjects()                     { return S_OK; }
  258.     virtual HRESULT FrameMove()                                { return S_OK; }
  259.     virtual HRESULT Render()                                   { return S_OK; }
  260.     virtual HRESULT InvalidateDeviceObjects()                  { return S_OK; }
  261.     virtual HRESULT DeleteDeviceObjects()                      { return S_OK; }
  262.     virtual HRESULT FinalCleanup()                             { return S_OK; }
  263.  
  264. protected:
  265.     SaverMode       m_SaverMode;         // sm_config, sm_full, sm_preview, etc.
  266.     BOOL            m_bAllScreensSame;   // If TRUE, show same image on all screens
  267.     HWND            m_hWnd;              // Focus window and device window on primary
  268.     HWND            m_hWndParent;
  269.     HINSTANCE       m_hInstance;
  270.     BOOL            m_bWaitForInputIdle;  // Used to pause when preview starts
  271.     DWORD           m_dwSaverMouseMoveCount;
  272.     BOOL            m_bIs9x;
  273.     HINSTANCE       m_hPasswordDLL;
  274.     VERIFYPWDPROC   m_VerifySaverPassword;
  275.     BOOL            m_bCheckingSaverPassword;
  276.     BOOL            m_bWindowed;
  277.  
  278.     // Variables for non-fatal error management
  279.     BOOL            m_bErrorMode;        // Whether to display an error
  280.     HRESULT         m_hrError;           // Error code to display
  281.     TCHAR           m_szError[400];      // Error message text
  282.  
  283.     MonitorInfo     m_Monitors[MAX_DISPLAYS];
  284.     DWORD           m_dwNumMonitors;
  285.     RenderUnit      m_RenderUnits[MAX_DISPLAYS];
  286.     DWORD           m_dwNumRenderUnits;
  287.     D3DAdapterInfo* m_Adapters[MAX_DISPLAYS];
  288.     DWORD           m_dwNumAdapters;
  289.     IDirect3D8*     m_pD3D;
  290.     IDirect3DDevice8* m_pd3dDevice;      // Current D3D device
  291.     RECT            m_rcRenderTotal;     // Rect of entire area to be rendered
  292.     RECT            m_rcRenderCurDevice; // Rect of render area of current device
  293.     D3DSURFACE_DESC m_d3dsdBackBuffer;   // Info on back buffer for current device
  294.  
  295.     TCHAR           m_strWindowTitle[200]; // Title for the app's window
  296.     BOOL            m_bAllowRef;         // Whether to allow REF D3D device
  297.     BOOL            m_bUseDepthBuffer;   // Whether to autocreate depthbuffer
  298.     BOOL            m_bMultithreaded;    // Whether to make D3D thread-safe
  299.     BOOL            m_bOneScreenOnly;    // Only ever show screensaver on one screen
  300.     TCHAR           m_strRegPath[200];   // Where to store registry info
  301.     DWORD           m_dwMinDepthBits;    // Minimum number of bits needed in depth buffer
  302.     DWORD           m_dwMinStencilBits;  // Minimum number of bits needed in stencil buffer
  303.     D3DSWAPEFFECT   m_SwapEffectFullscreen; // SwapEffect to use in fullscreen Present()
  304.     D3DSWAPEFFECT   m_SwapEffectWindowed; // SwapEffect to use in windowed Present()
  305.  
  306.     // Variables for timing
  307.     FLOAT           m_fTime;             // Current time in seconds
  308.     FLOAT           m_fElapsedTime;      // Time elapsed since last frame
  309.     FLOAT           m_fFPS;              // Instanteous frame rate
  310.     TCHAR           m_strDeviceStats[90];// D3D device stats for current device
  311.     TCHAR           m_strFrameStats[40]; // Frame stats for current device
  312. };
  313.  
  314. #endif
  315.